<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-present http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use app\admin\model\Menu;
use think\facade\Db;

class MainController extends AdminBaseController
{

    /**
     *  后台欢迎页
     */
    public function index(){
        $nowtime=time();
        //当天0点
        $today=date("Ymd",$nowtime);
        $today_start=strtotime($today);
        //当天 23:59:59
        $today_end=strtotime("{$today} + 1 day");
        
        $yesterday_start=$today_start - 60*60*24;
        
        
        /* 充值统计 */
        /* 支付金额 */
        $pay_money=Db::name('charge_user')->where("status=1")->sum('money');
        if(!$pay_money){
            $pay_money=0;
        }
        
        $pay_money_t=Db::name('charge_user')->where("status=1 and addtime >= {$today_start} and addtime < {$today_end}")->sum('money');
        if(!$pay_money_t){
            $pay_money_t=0;
        }
        
        $pay_money_y=Db::name('charge_user')->where("status=1 and addtime >= {$yesterday_start} and addtime < {$today_start}")->sum('money');
        if(!$pay_money_y){
            $pay_money_y=0;
        }
        
        $pay_money_rate='0%';
        $pay_money_rate_c='';
        if($pay_money_t==0 && $pay_money_y==0){
            
        }else if($pay_money_t==0){
            $pay_money_rate=$pay_money_y.'%';
            $pay_money_rate_c='down';
        }else if($pay_money_y==0){
            $pay_money_rate=$pay_money_t.'%';
            $pay_money_rate_c='up';
        }else{
            $rate=floor(($pay_money_t - $pay_money_y)/$pay_money_y*100);
            if($rate>0){
                $pay_money_rate_c='up';
            }else if($rate<0){
                $pay_money_rate_c='down';
            }
            $pay_money_rate=abs($rate).'%';
        }
        
        $this->assign('pay_money', $pay_money);
        $this->assign('pay_money_t', $pay_money_t);
        $this->assign('pay_money_y', $pay_money_y);
        $this->assign('pay_money_rate', $pay_money_rate);
        $this->assign('pay_money_rate_c', $pay_money_rate_c);
        
        /* 支付订单 */
        $pay_orders=Db::name('charge_user')->where("status=1")->count();
        if(!$pay_orders){
            $pay_orders=0;
        }
        
        $pay_orders_t=Db::name('charge_user')->where("status=1 and addtime >= {$today_start} and addtime < {$today_end}")->count();
        if(!$pay_orders_t){
            $pay_orders_t=0;
        }
        
        $pay_orders_y=Db::name('charge_user')->where("status=1 and addtime >= {$yesterday_start} and addtime < {$today_start}")->count();
        if(!$pay_orders_y){
            $pay_orders_y=0;
        }
        
        $pay_orders_rate='0%';
        $pay_orders_rate_c='';
        if($pay_orders_t==0 && $pay_orders_y==0){
            
        }else if($pay_orders_t==0){
            $pay_orders_rate=$pay_orders_y.'%';
            $pay_orders_rate_c='down';
        }else if($pay_orders_y==0){
            $pay_orders_rate=$pay_orders_t.'%';
            $pay_orders_rate_c='up';
        }else{
            $rate=floor(($pay_orders_t - $pay_orders_y)/$pay_orders_y*100);
            if($rate>0){
                $pay_orders_rate_c='up';
            }else if($rate<0){
                $pay_orders_rate_c='down';
            }
            $pay_orders_rate=abs($rate).'%';
        }
        
        $this->assign('pay_orders', $pay_orders);
        $this->assign('pay_orders_t', $pay_orders_t);
        $this->assign('pay_orders_y', $pay_orders_y);
        $this->assign('pay_orders_rate', $pay_orders_rate);
        $this->assign('pay_orders_rate_c', $pay_orders_rate_c);
        
        /* 支付人数 */
        $pay_users=Db::name('charge_user')->where("status=1")->group('uid')->count();
        if(!$pay_users){
            $pay_users=0;
        }
        
        $pay_users_t=Db::name('charge_user')->where("status=1 and addtime >= {$today_start} and addtime < {$today_end}")->group('uid')->count();
        if(!$pay_users_t){
            $pay_users_t=0;
        }
        
        $pay_users_y=Db::name('charge_user')->where("status=1 and addtime >= {$yesterday_start} and addtime < {$today_start}")->group('uid')->count();
        if(!$pay_users_y){
            $pay_users_y=0;
        }
        
        $pay_users_rate='0%';
        $pay_users_rate_c='';
        if($pay_users_t==0 && $pay_users_y==0){
            
        }else if($pay_users_t==0){
            $pay_users_rate=$pay_users_y.'%';
            $pay_users_rate_c='down';
        }else if($pay_users_y==0){
            $pay_users_rate=$pay_users_t.'%';
            $pay_users_rate_c='up';
        }else{
            $rate=floor(($pay_users_t - $pay_users_y)/$pay_users_y*100);
            if($rate>0){
                $pay_users_rate_c='up';
            }else if($rate<0){
                $pay_users_rate_c='down';
            }
            $pay_users_rate=abs($rate).'%';
        }
        
        $this->assign('pay_users', $pay_users);
        $this->assign('pay_users_t', $pay_users_t);
        $this->assign('pay_users_y', $pay_users_y);
        $this->assign('pay_users_rate', $pay_users_rate);
        $this->assign('pay_users_rate_c', $pay_users_rate_c);
        
        /* 注册统计 */
        /* 注册人数 */
        $users=Db::name('user')->where("user_type=2")->count();
        if(!$users){
            $users=0;
        }
        
        $users_t=Db::name('user')->where("user_type=2 and create_time >= {$today_start} and create_time < {$today_end}")->count();
        if(!$users_t){
            $users_t=0;
        }
        
        $users_y=Db::name('user')->where("user_type=2 and create_time >= {$yesterday_start} and create_time < {$today_start}")->count();
        if(!$users_y){
            $users_y=0;
        }
        
        $users_rate='0%';
        $users_rate_c='';
        if($users_t==0 && $users_y==0){
            
        }else if($users_t==0){
            $users_rate=$users_y.'%';
            $users_rate_c='down';
        }else if($users_y==0){
            $users_rate=$users_t.'%';
            $users_rate_c='up';
        }else{
            $rate=floor(($users_t - $users_y)/$users_y*100);
            if($rate>0){
                $users_rate_c='up';
            }else if($rate<0){
                $users_rate_c='down';
            }
            $users_rate=abs($rate).'%';
        }
        
        $this->assign('users', $users);
        $this->assign('users_t', $users_t);
        $this->assign('users_y', $users_y);
        $this->assign('users_rate', $users_rate);
        $this->assign('users_rate_c', $users_rate_c);
        
        /* 代理 */
        /* 邀请 */
        $agent=Db::name('agent')->count();
        if(!$agent){
            $agent=0;
        }
        
        $agent_t=Db::name('agent')->where("addtime >= {$today_start} and addtime < {$today_end}")->count();
        if(!$agent_t){
            $agent_t=0;
        }
        
        $agent_y=Db::name('agent')->where("addtime >= {$yesterday_start} and addtime < {$today_start}")->count();
        if(!$agent_y){
            $agent_y=0;
        }
        
        $agent_rate='0%';
        $agent_rate_c='';
        if($agent_t==0 && $agent_y==0){
            
        }else if($agent_t==0){
            $agent_rate=$agent_y.'%';
            $agent_rate_c='down';
        }else if($agent_y==0){
            $agent_rate=$agent_t.'%';
            $agent_rate_c='up';
        }else{
            $rate=floor(($agent_t - $agent_y)/$agent_y*100);
            if($rate>0){
                $agent_rate_c='up';
            }else if($rate<0){
                $agent_rate_c='down';
            }
            $agent_rate=abs($rate).'%';
        }
        
        $this->assign('agent', $agent);
        $this->assign('agent_t', $agent_t);
        $this->assign('agent_y', $agent_y);
        $this->assign('agent_rate', $agent_rate);
        $this->assign('agent_rate_c', $agent_rate_c);
        
        /* 消费统计 */
        /* 消费 */
        $coins=Db::name('user_coinrecord')->where("isdeduct=0")->sum('totalcoin');
        if(!$coins){
            $coins=0;
        }
        
        $coins_t=Db::name('user_coinrecord')->where("isdeduct=0 and addtime >= {$today_start} and addtime < {$today_end}")->sum('totalcoin');
        if(!$coins_t){
            $coins_t=0;
        }
        
        $coins_y=Db::name('user_coinrecord')->where("isdeduct=0 and addtime >= {$yesterday_start} and addtime < {$today_start}")->sum('totalcoin');
        if(!$coins_y){
            $coins_y=0;
        }
        
        $coins_rate='0%';
        $coins_rate_c='';
        if($coins_t==0 && $coins_y==0){
            
        }else if($coins_t==0){
            $coins_rate=$coins_y.'%';
            $coins_rate_c='down';
        }else if($coins_y==0){
            $coins_rate=$coins_t.'%';
            $coins_rate_c='up';
        }else{
            $rate=floor(($coins_t - $coins_y)/$coins_y*100);
            if($rate>0){
                $coins_rate_c='up';
            }else if($rate<0){
                $coins_rate_c='down';
            }
            $coins_rate=abs($rate).'%';
        }
        
        $this->assign('coins', $coins);
        $this->assign('coins_t', $coins_t);
        $this->assign('coins_y', $coins_y);
        $this->assign('coins_rate', $coins_rate);
        $this->assign('coins_rate_c', $coins_rate_c);
        
        /* 收益 */
        $votes=Db::name('user_coinrecord')->where("isdeduct=0 and action!=6 and action!=7")->sum('totalcoin');
        if(!$votes){
            $votes=0;
        }
        
        $votes_t=Db::name('user_coinrecord')->where("isdeduct=0 and action!=6 and action!=7 and addtime >= {$today_start} and addtime < {$today_end}")->sum('totalcoin');
        if(!$votes_t){
            $votes_t=0;
        }
        
        $votes_y=Db::name('user_coinrecord')->where("isdeduct=0 and action!=6 and action!=7 and addtime >= {$yesterday_start} and addtime < {$today_start}")->sum('totalcoin');
        if(!$votes_y){
            $votes_y=0;
        }
        
        $votes_rate='0%';
        $votes_rate_c='';
        if($votes_t==0 && $votes_y==0){
            
        }else if($votes_t==0){
            $votes_rate=$votes_y.'%';
            $votes_rate_c='down';
        }else if($votes_y==0){
            $votes_rate=$votes_t.'%';
            $votes_rate_c='up';
        }else{
            $rate=floor(($votes_t - $votes_y)/$votes_y*100);
            if($rate>0){
                $votes_rate_c='up';
            }else if($rate<0){
                $votes_rate_c='down';
            }
            $votes_rate=abs($rate).'%';
        }
        
        $this->assign('votes', $votes);
        $this->assign('votes_t', $votes_t);
        $this->assign('votes_y', $votes_y);
        $this->assign('votes_rate', $votes_rate);
        $this->assign('votes_rate_c', $votes_rate_c);
        
        /* 提现 */
        $cash=Db::name('cash_record')->where("status=1 ")->sum('money');
        if(!$cash){
            $cash=0;
        }
        
        $cash_t=Db::name('cash_record')->where("status=1 and uptime >= {$today_start} and uptime < {$today_end}")->sum('money');
        if(!$cash_t){
            $cash_t=0;
        }
        
        $cash_y=Db::name('cash_record')->where("status=1 and uptime >= {$yesterday_start} and uptime < {$today_start}")->sum('money');
        if(!$cash_y){
            $cash_y=0;
        }
        
        $cash_rate='0%';
        $cash_rate_c='';
        if($cash_t==0 && $cash_y==0){
            
        }else if($cash_t==0){
            $cash_rate=$cash_y.'%';
            $cash_rate_c='down';
        }else if($cash_y==0){
            $cash_rate=$cash_t.'%';
            $cash_rate_c='up';
        }else{
            $rate=floor(($cash_t - $cash_y)/$cash_y*100);
            if($rate>0){
                $cash_rate_c='up';
            }else if($rate<0){
                $cash_rate_c='down';
            }
            $cash_rate=abs($rate).'%';
        }
        $this->assign('cash', $cash);
        $this->assign('cash_t', $cash_t);
        $this->assign('cash_y', $cash_y);
        $this->assign('cash_rate', $cash_rate);
        $this->assign('cash_rate_c', $cash_rate_c);
        
        
        /* 审核统计 */
        /* 视频 */
        $videos=Db::name('video')->where("status=0 ")->count();
        if(!$videos){
            $videos=0;
        }
        
        $videos_t=Db::name('video')->where("status=0 and addtime >= {$today_start} and addtime < {$today_end}")->count();
        if(!$videos_t){
            $videos_t=0;
        }
        
        $videos_y=Db::name('video')->where("status=0 and addtime >= {$yesterday_start} and addtime < {$today_start}")->count();
        if(!$videos_y){
            $videos_y=0;
        }
        
        $videos_rate='0%';
        $videos_rate_c='';
        if($videos_t==0 && $videos_y==0){
            
        }else if($videos_t==0){
            $videos_rate=$videos_y.'%';
            $videos_rate_c='down';
        }else if($videos_y==0){
            $videos_rate=$videos_t.'%';
            $videos_rate_c='up';
        }else{
            $rate=floor(($videos_t - $videos_y)/$videos_y*100);
            if($rate>0){
                $videos_rate_c='up';
            }else if($rate<0){
                $videos_rate_c='down';
            }
            $videos_rate=abs($rate).'%';
        }
        $this->assign('videos', $videos);
        $this->assign('videos_t', $videos_t);
        $this->assign('videos_y', $videos_y);
        $this->assign('videos_rate', $videos_rate);
        $this->assign('videos_rate_c', $videos_rate_c);
        
        /* 私照 */
        $photos=Db::name('photo')->where("status=0 ")->count();
        if(!$photos){
            $photos=0;
        }
        
        $photos_t=Db::name('photo')->where("status=0 and addtime >= {$today_start} and addtime < {$today_end}")->count();
        if(!$photos_t){
            $photos_t=0;
        }
        
        $photos_y=Db::name('photo')->where("status=0 and addtime >= {$yesterday_start} and addtime < {$today_start}")->count();
        if(!$photos_y){
            $photos_y=0;
        }
        
        $photos_rate='0%';
        $photos_rate_c='';
        if($photos_t==0 && $photos_y==0){
            
        }else if($photos_t==0){
            $photos_rate=$photos_y.'%';
            $photos_rate_c='down';
        }else if($photos_y==0){
            $photos_rate=$photos_t.'%';
            $photos_rate_c='up';
        }else{
            $rate=floor(($photos_t - $photos_y)/$photos_y*100);
            if($rate>0){
                $photos_rate_c='up';
            }else if($rate<0){
                $photos_rate_c='down';
            }
            $photos_rate=abs($rate).'%';
        }
        $this->assign('photos', $photos);
        $this->assign('photos_t', $photos_t);
        $this->assign('photos_y', $photos_y);
        $this->assign('photos_rate', $photos_rate);
        $this->assign('photos_rate_c', $photos_rate_c);
        
        /* 认证 */
        $auths=Db::name('author_auth')->where("status=0 ")->count();
        if(!$auths){
            $auths=0;
        }
        
        $auths_t=Db::name('author_auth')->where("status=0 and addtime >= {$today_start} and addtime < {$today_end}")->count();
        if(!$auths_t){
            $auths_t=0;
        }
        
        $auths_y=Db::name('author_auth')->where("status=0 and addtime >= {$yesterday_start} and addtime < {$today_start}")->count();
        if(!$auths_y){
            $auths_y=0;
        }
        
        $auths_rate='0%';
        $auths_rate_c='';
        if($auths_t==0 && $auths_y==0){
            
        }else if($auths_t==0){
            $auths_rate=$auths_y.'%';
            $auths_rate_c='down';
        }else if($auths_y==0){
            $auths_rate=$auths_t.'%';
            $auths_rate_c='up';
        }else{
            $rate=floor(($auths_t - $auths_y)/$auths_y*100);
            if($rate>0){
                $auths_rate_c='up';
            }else if($rate<0){
                $auths_rate_c='down';
            }
            $auths_rate=abs($rate).'%';
        }
        $this->assign('auths', $auths);
        $this->assign('auths_t', $auths_t);
        $this->assign('auths_y', $auths_y);
        $this->assign('auths_rate', $auths_rate);
        $this->assign('auths_rate_c', $auths_rate_c);
        
        /* 通话统计 */
        /* 语音时间 */
        $live_voice_a=Db::name('conversa_log')->field("sum(endtime) as endtotal,sum(starttime) as starttotal")->where("status=2 and type=2 and endtime!=0 and starttime!=0 ")->find();
        $live_voice=0;
        if($live_voice_a){
            $live_voice=$live_voice_a['endtotal']-$live_voice_a['starttotal'];
        }
        
        $live_voice_t_a=Db::name('conversa_log')->field("sum(endtime) as endtotal,sum(starttime) as starttotal")->where("status=2 and type=2 and endtime!=0 and starttime!=0 and starttime >= {$today_start} and starttime < {$today_end}")->find();
        $live_voice_t=0;
        if($live_voice_t_a){
            $live_voice_t=$live_voice_t_a['endtotal']-$live_voice_t_a['starttotal'];
        }
        
        $live_voice_y_a=Db::name('conversa_log')->field("sum(endtime) as endtotal,sum(starttime) as starttotal")->where("status=2 and type=2 and endtime!=0 and starttime!=0 and starttime >= {$yesterday_start} and starttime < {$today_start}")->find();
        $live_voice_y=0;
        if($live_voice_y_a){
            $live_voice_y=$live_voice_y_a['endtotal']-$live_voice_y_a['starttotal'];
        }
        
        $live_voice_rate='0%';
        $live_voice_rate_c='';
        if($live_voice_t==0 && $live_voice_y==0){
            
        }else if($live_voice_t==0){
            $live_voice_rate=$live_voice_y.'%';
            $live_voice_rate_c='down';
        }else if($live_voice_y==0){
            $live_voice_rate=$live_voice_t.'%';
            $live_voice_rate_c='up';
        }else{
            $rate=floor(($live_voice_t - $live_voice_y)/$live_voice_y*100);
            if($rate>0){
                $live_voice_rate_c='up';
            }else if($rate<0){
                $live_voice_rate_c='down';
            }
            $live_voice_rate=abs($rate).'%';
        }
        $this->assign('live_voice', floor($live_voice/60));
        $this->assign('live_voice_t', floor($live_voice_t/60));
        $this->assign('live_voice_y', floor($live_voice_y/60));
        $this->assign('live_voice_rate', $live_voice_rate);
        $this->assign('live_voice_rate_c', $live_voice_rate_c);
        
        /* 视频时间 */
        $live_video_a=Db::name('conversa_log')->field("sum(endtime) as endtotal,sum(starttime) as starttotal")->where("status=2 and type=1 and endtime!=0 and starttime!=0 ")->find();
        $live_video=0;
        if($live_video_a){
            $live_video=$live_video_a['endtotal']-$live_video_a['starttotal'];
        }
        
        $live_video_t_a=Db::name('conversa_log')->field("sum(endtime) as endtotal,sum(starttime) as starttotal")->where("status=2 and type=1 and endtime!=0 and starttime!=0 and starttime >= {$today_start} and starttime < {$today_end}")->find();
        $live_video_t=0;
        if($live_video_t_a){
            $live_video_t=$live_video_t_a['endtotal']-$live_video_t_a['starttotal'];
        }
        
        $live_video_y_a=Db::name('conversa_log')->field("sum(endtime) as endtotal,sum(starttime) as starttotal")->where("status=2 and type=1 and endtime!=0 and starttime!=0 and starttime >= {$yesterday_start} and starttime < {$today_start}")->find();
        $live_video_y=0;
        if($live_video_y_a){
            $live_video_y=$live_video_y_a['endtotal']-$live_video_y_a['starttotal'];
        }
        
        $live_video_rate='0%';
        $live_video_rate_c='';
        if($live_video_t==0 && $live_video_y==0){
            
        }else if($live_video_t==0){
            $live_video_rate=$live_video_y.'%';
            $live_video_rate_c='down';
        }else if($live_video_y==0){
            $live_video_rate=$live_video_t.'%';
            $live_video_rate_c='up';
        }else{
            $rate=floor(($live_video_t - $live_video_y)/$live_video_y*100);
            if($rate>0){
                $live_video_rate_c='up';
            }else if($rate<0){
                $live_video_rate_c='down';
            }
            $live_video_rate=abs($rate).'%';
        }
        $this->assign('live_video',  floor($live_video/60));
        $this->assign('live_video_t',  floor($live_video_t/60));
        $this->assign('live_video_y',  floor($live_video_y/60));
        $this->assign('live_video_rate', $live_video_rate);
        $this->assign('live_video_rate_c', $live_video_rate_c);
        
        /* 总通话 */
        $lives=$live_voice + $live_video;
        
        $lives_t=$live_voice_t + $live_video_t;
        
        $lives_y=$live_voice_y + $live_video_y;
        
        $lives_rate='0%';
        $lives_rate_c='';
        if($lives_t==0 && $lives_y==0){
            
        }else if($lives_t==0){
            $lives_rate=$lives_y.'%';
            $lives_rate_c='down';
        }else if($lives_y==0){
            $lives_rate=$lives_t.'%';
            $lives_rate_c='up';
        }else{
            $rate=floor(($lives_t - $lives_y)/$lives_y*100);
            if($rate>0){
                $lives_rate_c='up';
            }else if($rate<0){
                $lives_rate_c='down';
            }
            $lives_rate=abs($rate).'%';
        }
        $this->assign('lives', floor($lives/60));
        $this->assign('lives_t', floor($lives_t/60));
        $this->assign('lives_y', floor($lives_y/60));
        $this->assign('lives_rate', $lives_rate);
        $this->assign('lives_rate_c', $lives_rate_c);
        

        return $this->fetch();
    }
}
